/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * altaUsuario.java
 *
 * Created on 21-abr-2011, 16:19:15
 */

package interfaces;

import clinica.ClinicaView;
import java.awt.CardLayout;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;
import org.jdesktop.application.FrameView;
import Controlador.*;
import GestorClinico.Direccion;
import GestorUsuarios.EstadoUsuario;
import GestorUsuarios.Farmaceutico;
import GestorUsuarios.GeneroUsuario;
import GestorUsuarios.TipoUsuario;
import GestorUsuarios.Usuario;
import clinica.Interfaces;
import clinica.Interfaz;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Victor
 */
public class altaUsuario extends javax.swing.JPanel {

    private ClinicaView app;

    /** Creates new form altaUsuario */
    public altaUsuario(ClinicaView a) {
        initComponents();
        app = a;
        direcciones.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        direcciones.setRowHeight(32);
    }

    public void addDireccion(String direccion, String localidad, String CP, String pais, String telefono) {
        String [] valores = {direccion,localidad,CP,pais,telefono};
            ((DefaultTableModel)direcciones.getModel()).addRow(valores);
    }

    public void modDireccion(int dir, String direccion, String localidad, String CP, String pais, String telefono) {
            ((DefaultTableModel)direcciones.getModel()).setValueAt(direccion, dir, 0);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(localidad, dir, 1);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(CP, dir, 2);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(pais, dir, 3);
            ((DefaultTableModel)direcciones.getModel()).setValueAt(telefono, dir, 4);
    }

    public void limpiaDatos() {
        nomb.setText("");
        apell.setText("");
        correoele.setText("");
        DNIpass.setText("");
        telefono.setText("");
        fecha.setDate(null);
        tipo.setSelectedIndex(0);
        login.setText("");
        hombre.setSelected(true);
        Interfaz.limpiarTabla(direcciones);
 
    }

    public void altaUsuario() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        GeneroUsuario genero;
            if(hombre.isSelected())
                genero = GeneroUsuario.MASCULINO;
            else genero = GeneroUsuario.FEMENINO;

        if(tipo.getSelectedIndex()==1) {
            Usuario usuario = new Usuario(DNIpass.getText(), nomb.getText(), apell.getText(),
                     fecha.getDate(), genero, correoele.getText(), Integer.parseInt(telefono.getText()),
                     login.getText(), DNIpass.getText()+nomb.getText().substring(0,2),
                     EstadoUsuario.ACTIVO, TipoUsuario.MEDICO,null);

            app.abrirAltaUsuario2(usuario,DNIpass.getText()+nomb.getText().substring(0,2), getDirecciones());
        } else {


            TipoUsuario tipousu = null;
            if(tipo.getSelectedIndex()==0)
                tipousu = TipoUsuario.PACIENTE;
            else if(tipo.getSelectedIndex()==1)
                tipousu = TipoUsuario.MEDICO;
            else if(tipo.getSelectedIndex()==2)
                tipousu = TipoUsuario.FARMACEUTICO;
            else if(tipo.getSelectedIndex()==3)
                tipousu = TipoUsuario.GESTOR;
            else if(tipo.getSelectedIndex()==4)
                tipousu = TipoUsuario.TECNICOPRUEBAS;

            String res = Controlador.altaUsuario(DNIpass.getText(), nomb.getText(), apell.getText(),
                    genero, correoele.getText(), getDirecciones(), Integer.parseInt(telefono.getText()),
                    fecha.getDate(), tipousu, login.getText(), DNIpass.getText()+nomb.getText().substring(0,2), null);
            if(res==null)
                app.abrirMensajeExito("Usuario dado de alta correctamente. Contraseña: "+DNIpass.getText()+nomb.getText().substring(0,2), Interfaces.PRINCIPAL_GESTOR);
            else app.abrirMensajeError(res, Interfaces.PRINCIPAL_GESTOR);
        }
    }


    public Direccion [] getDirecciones() {
        int rowCount = direcciones.getRowCount();

        if(rowCount==0)
            return null;

        Direccion [] dires = new Direccion[rowCount];
        for(int i=0;i<rowCount;i++) {
            String direccion = (String) ((DefaultTableModel)direcciones.getModel()).getValueAt( i, 0);
            String localidad = (String) ((DefaultTableModel)direcciones.getModel()).getValueAt( i, 1);
            int CP = Integer.parseInt((String)((DefaultTableModel)direcciones.getModel()).getValueAt( i, 2));
            String pais = (String) ((DefaultTableModel)direcciones.getModel()).getValueAt( i, 3);
            int telefono = Integer.parseInt((String)((DefaultTableModel)direcciones.getModel()).getValueAt( i, 4));

            dires[i] = new Direccion(direccion, localidad, CP, pais, telefono);
        }

        return dires;
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        aceptar3 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        apell = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        correoele = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        nomb = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        DNIpass = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        telefono = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        login = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        tipo = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        hombre = new javax.swing.JRadioButton();
        mujer = new javax.swing.JRadioButton();
        jPanel11 = new javax.swing.JPanel();
        jPanel10 = new javax.swing.JPanel();
        fecha = new com.toedter.calendar.JDateChooser();
        jPanel5 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        direcciones = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel6 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(780, 472));
        setName("Form"); // NOI18N
        setPreferredSize(new java.awt.Dimension(780, 472));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(altaUsuario.class, this);
        aceptar3.setAction(actionMap.get("aceptar")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(altaUsuario.class);
        aceptar3.setFont(resourceMap.getFont("aceptar3.font")); // NOI18N
        aceptar3.setIcon(resourceMap.getIcon("aceptar3.icon")); // NOI18N
        aceptar3.setName("aceptar3"); // NOI18N
        aceptar3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptar3ActionPerformed(evt);
            }
        });

        jTabbedPane1.setFont(Interfaz.setFontSize());
        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        jPanel4.setName("jPanel4"); // NOI18N

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        apell.setFont(Interfaz.setFontSize());
        apell.setName("apell"); // NOI18N

        jLabel3.setFont(Interfaz.setFontSize());
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        correoele.setFont(Interfaz.setFontSize());
        correoele.setName("correoele"); // NOI18N

        jLabel4.setFont(Interfaz.setFontSize());
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        nomb.setFont(Interfaz.setFontSize());
        nomb.setName("nomb"); // NOI18N

        jLabel5.setFont(Interfaz.setFontSize());
        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        DNIpass.setFont(Interfaz.setFontSize());
        DNIpass.setName("DNIpass"); // NOI18N

        jLabel6.setFont(Interfaz.setFontSize());
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        telefono.setFont(Interfaz.setFontSize());
        telefono.setName("telefono"); // NOI18N

        jLabel7.setFont(Interfaz.setFontSize());
        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel1.setFont(Interfaz.setFontSize());
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        login.setFont(Interfaz.setFontSize());
        login.setName("login"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, resourceMap.getString("jPanel2.border.title"), javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize())); // NOI18N
        jPanel2.setName("jPanel2"); // NOI18N

        tipo.setFont(Interfaz.setFontSize());
        tipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Paciente", "Médico", "Farmaceutico", "Gestor", "Técnico" }));
        tipo.setName("tipo"); // NOI18N

        jLabel8.setIcon(resourceMap.getIcon("jLabel8.icon")); // NOI18N
        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel8)
                .addGap(18, 18, 18)
                .addComponent(tipo, 0, 167, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(tipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel8))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Sexo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel1.setName("jPanel1"); // NOI18N

        buttonGroup1.add(hombre);
        hombre.setFont(Interfaz.setFontSize());
        hombre.setText(resourceMap.getString("hombre.text")); // NOI18N
        hombre.setName("hombre"); // NOI18N

        buttonGroup1.add(mujer);
        mujer.setFont(Interfaz.setFontSize());
        mujer.setText(resourceMap.getString("mujer.text")); // NOI18N
        mujer.setName("mujer"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(hombre)
                    .addComponent(mujer))
                .addContainerGap(184, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(hombre)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(mujer)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel11.setName("jPanel11"); // NOI18N

        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
        jPanel11.setLayout(jPanel11Layout);
        jPanel11Layout.setHorizontalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 427, Short.MAX_VALUE)
        );
        jPanel11Layout.setVerticalGroup(
            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 17, Short.MAX_VALUE)
        );

        jPanel10.setName("jPanel10"); // NOI18N

        javax.swing.GroupLayout jPanel10Layout = new javax.swing.GroupLayout(jPanel10);
        jPanel10.setLayout(jPanel10Layout);
        jPanel10Layout.setHorizontalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 724, Short.MAX_VALUE)
        );
        jPanel10Layout.setVerticalGroup(
            jPanel10Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        fecha.setFont(Interfaz.setFontSize());
        fecha.setName("fecha"); // NOI18N

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel4Layout.createSequentialGroup()
                                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                    .addGroup(jPanel4Layout.createSequentialGroup()
                                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                                        .addGap(8, 8, 8)))
                                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(correoele, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                                    .addComponent(fecha, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                                    .addComponent(DNIpass, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                                    .addComponent(telefono, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                                    .addComponent(nomb, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                                    .addComponent(apell, javax.swing.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(login, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addComponent(jPanel10, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nomb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(apell, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(DNIpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5)
                        .addComponent(correoele, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(login, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(telefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6))
                        .addGap(14, 14, 14)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(fecha, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(119, 119, 119))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel4.TabConstraints.tabTitle"), jPanel4); // NOI18N

        jPanel5.setName("jPanel5"); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 724, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        direcciones.setFont(Interfaz.setFontSize());
        direcciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Direccion", "Localidad", "Cod.Postal", "Pais", "Telefono"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        direcciones.setName("direcciones"); // NOI18N
        jScrollPane1.setViewportView(direcciones);

        jButton1.setAction(actionMap.get("addDir")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("modDir")); // NOI18N
        jButton2.setIcon(resourceMap.getIcon("jButton2.icon")); // NOI18N
        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        jButton3.setAction(actionMap.get("eliDir")); // NOI18N
        jButton3.setIcon(resourceMap.getIcon("jButton3.icon")); // NOI18N
        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N

        jPanel6.setName("jPanel6"); // NOI18N

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 708, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 631, Short.MAX_VALUE)
                                .addGap(25, 25, 25)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addGap(84, 84, 84))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(83, 83, 83))
        );

        jTabbedPane1.addTab(resourceMap.getString("jPanel5.TabConstraints.tabTitle"), null, jPanel5, resourceMap.getString("jPanel5.TabConstraints.tabToolTip")); // NOI18N

        jButton4.setAction(actionMap.get("cancelar")); // NOI18N
        jButton4.setIcon(resourceMap.getIcon("jButton4.icon")); // NOI18N
        jButton4.setText(resourceMap.getString("jButton4.text")); // NOI18N
        jButton4.setName("jButton4"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 756, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(aceptar3)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(aceptar3)
                    .addComponent(jButton4))
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName(resourceMap.getString("jTabbedPane1.AccessibleContext.accessibleName")); // NOI18N
    }// </editor-fold>//GEN-END:initComponents

    private void aceptar3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptar3ActionPerformed

    }//GEN-LAST:event_aceptar3ActionPerformed

    @Action
    public void aceptar() throws NoSuchAlgorithmException, IOException, ClassNotFoundException, SQLException {
        // Comprobacion de errores
        boolean hay_errores = false;
        app.limpiaErrores();

        String expresion_numero = "^\\d{1,9}$";
	Pattern pattern = Pattern.compile(expresion_numero);
	Matcher matcher = pattern.matcher(telefono.getText());

        if(nomb.getText().length()< 4) {
            app.addError("El nombre debe tener más de 4 caracteres.");
            hay_errores = true;
        }
        if(nomb.getText().length() > 64) {
            app.addError("El nombre debe tener menos de 64 caracteres.");
            hay_errores = true;
        }

        if(apell.getText().length()< 4) {
            app.addError("El apellido debe tener más de 4 caracteres.");
            hay_errores = true;
        }
        if(apell.getText().length() > 128) {
            app.addError("El apellido debe tener menos de 128 caracteres.");
            hay_errores = true;
        }

        if(DNIpass.getText().length()< 4) {
            app.addError("El DNI o pasaporte debe tener más de 4 caracteres.");
            hay_errores = true;
        }
        if(DNIpass.getText().length() > 16) {
            app.addError("El DNI o pasaporte debe tener menos de 16 caracteres.");
            hay_errores = true;
        }

        if(correoele.getText().length() == 0) {
            app.addError("Introduzca el correo.");
            hay_errores = true;
        }
        if(correoele.getText().length() > 256) {
            app.addError("El correo debe tener menos de 256 caracteres.");
            hay_errores = true;
        }

        if(login.getText().length() < 4) {
            app.addError("El login debe tener más de 4 caracteres.");
            hay_errores = true;
        }
        if(login.getText().length() > 32) {
            app.addError("El login debe tener menos de 32 caracteres.");
            hay_errores = true;
        }

        if(fecha.getDate()==null) {
            app.addError("Debe seleccionar una fecha de nacimiento.");
            hay_errores = true;
        }
        
        Calendar hoy = Calendar.getInstance();
        if(fecha.getDate().after(hoy.getTime())) {
            app.addError("La fecha es mayor que el día actual.");
            hay_errores = true;
        }
        
        if(!matcher.matches()) {
            app.addError("El movil es incorrecto, debe contener 9 dígitos.");
            hay_errores = true;
        }

        if(hay_errores==true)
            app.muestraListaErrores();
        else this.altaUsuario();
    }

    @Action
    public void addDir() {
        app.getVenanaDirecciones().limpiaEntradas();
        app.getVenanaDirecciones().setPadre(this,null);
        app.getVenanaDirecciones().setVisible(true);
    }

    @Action
    public void cancelar() {
        app.abrirPrincipalGestor();
    }

    @Action
    public void modDir() {
        int row = direcciones.getSelectedRow();
        if(row>=0 && row<direcciones.getRowCount()) {
            TableModel m = direcciones.getModel();

            app.getVenanaDirecciones().rellenaEntradas((String)m.getValueAt(row, 0),
            (String)m.getValueAt(row, 1), (String)m.getValueAt(row, 2), (String)m.getValueAt(row, 3),
                    (String)m.getValueAt(row, 4),row);
            app.getVenanaDirecciones().setPadre(this,null);
            app.getVenanaDirecciones().setVisible(true);
        }

    }

    @Action
    public void eliDir() {
        int row = direcciones.getSelectedRow();
        if(row>=0 && row<direcciones.getRowCount())
            ((DefaultTableModel)direcciones.getModel()).removeRow(row);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField DNIpass;
    private javax.swing.JButton aceptar3;
    private javax.swing.JTextField apell;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField correoele;
    private javax.swing.JTable direcciones;
    private com.toedter.calendar.JDateChooser fecha;
    private javax.swing.JRadioButton hombre;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel11;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField login;
    private javax.swing.JRadioButton mujer;
    private javax.swing.JTextField nomb;
    private javax.swing.JTextField telefono;
    private javax.swing.JComboBox tipo;
    // End of variables declaration//GEN-END:variables

}
